package com.bskj.kuaifu.controller.web.api;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.UserInfo;
import com.bskj.common.constants.CommonConstatns;
import com.bskj.common.enums.ThirdPlatformTypeEnum;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.utils.ResultMapUtil;
import com.bskj.common.utils.StringUtils;
import com.bskj.common.utils.WeixinApiUtils;
import com.bskj.kuaifu.service.IUserInfoService;
import com.bskj.kuaifu.service.IUserThirdAuthService;

@Controller
@RequestMapping("api/userInfo")
public class UserInfoApi {
	@Autowired
	private IUserThirdAuthService userThirdAuthService;
	
	
	@Autowired
	private IUserInfoService userInfoService;
	
	@ResponseBody
	@RequestMapping("/weChat/insert")
	public Map<String, Object> insertUserInfo(String openId, String appId, String jsonWechatUser) throws Exception{
		Map<String, Object> resultMap =new HashMap<>();
		JSONObject jsonObject = JSONObject.parseObject(jsonWechatUser);
		UserInfo userInfo=userInfoService.getUserInfoByOpenId(openId);
		if(userInfo == null){
			userInfo = new UserInfo(); 
			userInfo.setName((String)jsonObject.get("nickname"));
	    	userInfo.setImage((String)jsonObject.get("headimgurl"));
	    	userInfo.setUnionid((String)jsonObject.get("unionid"));
	    	userInfo.setAppId(appId);
	    	userInfo.setOpenId(openId);
	    	userInfo.setCreateTime(new Date());
	    	userInfo.setUpdateTime(new Date());
	    	userInfoService.insert(userInfo);
		}else {
			if(StringUtils.isBank(userInfo.getUnionid())){
				userInfo.setAppId(appId);
				userInfo.setUnionid((String)jsonObject.get("unionid"));
				userInfo.setName((String) jsonObject.get("nickname"));
				userInfo.setImage((String) jsonObject.get("headimgurl"));
				userInfo.setUpdateTime(new Date());
				userInfoService.update(userInfo);
			}
		}
    	
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("/weChat/get")
	public Map<String, Object> getUserInfo( String openid,String unionid,int thirdPlatformType) throws Exception {
		ThirdPlatformTypeEnum type = ThirdPlatformTypeEnum.get(thirdPlatformType);
		if (type == null || unionid == null || unionid.equals("undefined") || unionid.equals("")) {
			return ResultMapUtil.genErrorMap("错误的请求类型");
		}
		Map<String, Object> resultMap=new HashMap<>();
		UserInfo userInfo = userThirdAuthService.getUserInfoWithWeInfo(CommonConstatns.KUAIFU_WX_SMALL_APP_APPID,
				openid, ThirdPlatformTypeEnum.We_Small_App);
		if(userInfo == null){
			resultMap = userThirdAuthService.saveThirdAuth(CommonConstatns.KUAIFU_WX_SMALL_APP_APPID, openid, unionid, type);	
		}else {
			resultMap = ResultMapUtil.genSuccessMap("小程序用户存在");
		}
		return resultMap;
	}
	
	
	@ResponseBody
	@RequestMapping("/weChat/getOpenid")
	public Map<String, Object> getOpenid(String code) throws Exception {
		return WeixinApiUtils.smallGetUserInfo(code);
	}
	
	
	/**
	 * 查询用户无感支付状态
	 * @param openid
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/queryUserStatus")
	public Map<String, Object> queryUserStatus(String openid, @RequestParam(required = false) String plateNumber)
			throws Exception {
		UserInfo userInfo = userThirdAuthService.getUserInfoWithWeInfo(CommonConstatns.KUAIFU_WX_SMALL_APP_APPID,
				openid, ThirdPlatformTypeEnum.We_Small_App);
		Integer userId = 0;
		if (userInfo != null) {
			userId = userInfo.getId();
		}

		Map<String, Object> resultMap = userInfoService.queryUserNoSenseStatus(userId, openid, plateNumber);
		LogUtils.info("queryUserStatus " + openid + " respone : " + resultMap);
		if (resultMap.get("queryResult") != null) {
			return ResultMapUtil.genSuccessDataMap(resultMap.get("queryResult"));
		} else {
			LogUtils.error("queryUserStatus " + openid + " respone : " + resultMap.get("queryResult"));
			return ResultMapUtil.genErrorMap("接口异常");
		}
	}
}
